package com.guxingke.typecho4j.widget.handle.content;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.Content;
import com.guxingke.typecho4j.route.DispatcherStrategy;
import com.guxingke.typecho4j.route.Route;
import com.guxingke.typecho4j.route.Routes;
import com.guxingke.typecho4j.util.DateUtil;
import com.guxingke.typecho4j.widget.WidgetHandle;
import com.guxingke.typecho4j.widget.layout.LayoutWidget;
import com.guxingke.typecho4j.widget.layout.front.IndexLayoutWidget;
import com.guxingke.typecho4j.widget.provider.OptionBaseWidget;
import com.jfinal.core.Controller;

/**
 * 更新编辑配置的处理组件
 * 
 * @author guxingke
 *
 */
public class PageEditHandleWidget extends OptionBaseWidget implements
		WidgetHandle {

	@Override
	public void handle(Controller controller) throws Typecho4jException {
		System.out.println("page edit handle");

		Content page = controller.getModel(Content.class, "page");

		if (page.get("cid") != null) {
			page.set("modified", DateUtil.getUnixTimestamp());
			page.update();
		} else {
			page.set("created", DateUtil.getUnixTimestamp());
			page.save();
		}

		LayoutWidget.refreshLayout(IndexLayoutWidget.class);
	}

	@Override
	public void configRoute(Routes routes) {
		Route updateEditOptionRoute = new Route("pageEdit",
				this.getClass(), "/admin/manage-pages",
				DispatcherStrategy.REDIRECT);
		routes.addRoute(updateEditOptionRoute);
	}
}
